home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Debug.java < prev    next >
Text File  |  1998-10-24  |  12KB  |  551 lines

  1. package com.symantec.itools.lang;
  2.  
  3.  
  4. import java.io.BufferedWriter;
  5. import java.io.InputStream;
  6. import java.io.IOException;
  7. import java.io.File;
  8. import java.io.FileDescriptor;
  9. import java.io.FileWriter;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.io.StringWriter;
  13. import java.io.PrintWriter;
  14. import java.io.Writer;
  15. import java.net.URL;
  16. import java.text.DateFormat;
  17. import java.util.Calendar;
  18. import com.symantec.itools.util.Properties;
  19.  
  20.  
  21. /**
  22.  * @author Symantec Internet Tools Division
  23.  * @version 1.0
  24.  * @since VCafe 3.0
  25.  */
  26.  
  27. public class Debug
  28. {
  29.     /**
  30.      * @since VCafe 3.0
  31.      */
  32.     public static final boolean DEBUG_OFF;
  33.  
  34.     /**
  35.      * @since VCafe 3.0
  36.      */
  37.     public static final boolean ASSERT_OFF;
  38.  
  39.     /**
  40.      * @since VCafe 3.0
  41.      */
  42.  
  43.     public static final int INFORMATION = 0x0001;
  44.  
  45.     /**
  46.      * @since VCafe 3.0
  47.      */
  48.     public static final int WARNING = 0x0002;
  49.  
  50.     /**
  51.      * @since VCafe 3.0
  52.      */
  53.     public static final int ERROR = 0x0004;
  54.  
  55.     /**
  56.      * @since VCafe 3.0
  57.      */
  58.     public static final int EXCEPTION = 0x0008;
  59.  
  60.     /**
  61.      * @since VCafe 3.0
  62.      */
  63.     public static final int TRACE = 0x0016;
  64.  
  65.     /**
  66.      * @since VCafe 3.0
  67.      */
  68.     public static final int ASSERT = 0x0032;
  69.  
  70.     /**
  71.      * @since VCafe 3.0
  72.      */
  73.     protected static boolean informationOff;
  74.     protected static boolean warningOff;
  75.     protected static boolean errorOff;
  76.     protected static boolean exceptionOff;
  77.     protected static boolean traceOff;
  78.     protected static boolean assertOff;
  79.  
  80.     /**
  81.      * @since VCafe 3.0
  82.      */
  83.     protected static PrintWriter writer;
  84.  
  85.     static
  86.     {
  87.         Properties properties;
  88.  
  89.         try
  90.         {
  91.             properties = new Properties("/com/symantec/itools/lang/Debug.properties");
  92.         }
  93.         catch(Throwable ex)
  94.         {
  95.             properties = new Properties();
  96.         }
  97.  
  98.         try
  99.         {
  100.             DEBUG_OFF  = properties.getBoolean("debug.off", true);
  101.         }
  102.         catch(Throwable ex)
  103.         {
  104.             DEBUG_OFF = true;
  105.         }
  106.  
  107.         try
  108.         {
  109.             ASSERT_OFF  = properties.getBoolean("assert.off", true);
  110.         }
  111.         catch(Throwable ex)
  112.         {
  113.             ASSERT_OFF = true;
  114.         }
  115.  
  116.         setInformationOff(properties.getBoolean("debug.information.off", false));
  117.         setWarningOff(properties.getBoolean("debug.warning.off", false));
  118.         setErrorOff(properties.getBoolean("debug.error.off", false));
  119.         setExceptionOff(properties.getBoolean("debug.excption.off", false));
  120.         setTraceOff(properties.getBoolean("debug.trace.off", true));
  121.         setAssertOff(properties.getBoolean("debug.assert.off", true));
  122.     }
  123.  
  124.     protected Debug()
  125.     {
  126.         throw new IllegalInstantiationError(Debug.class);
  127.     }
  128.  
  129.     /**
  130.      * @param str TODO
  131.      * @exception java.io.IOException
  132.      * @since VCafe 3.0
  133.      */
  134.  
  135.     public static void setWriter(String str)
  136.         throws IOException
  137.     {
  138.         if(writer == null)
  139.         {
  140.             if(str.equals("System.out"))
  141.             {
  142.                 setWriter(System.out);
  143.             }
  144.             else if(str.equals("System.err"))
  145.             {
  146.                 setWriter(System.err);
  147.             }
  148.             else
  149.             {
  150.                 setWriter(new FileWriter(str));
  151.             }
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * @param w TODO
  157.      * @since VCafe 3.0
  158.      */
  159.  
  160.     public static void setWriter(Writer w)
  161.     {
  162.         if(writer == null)
  163.         {
  164.             writer = new PrintWriter(new BufferedWriter(w), true);
  165.         }
  166.     }
  167.  
  168.     /**
  169.      * @param stream TODO
  170.      * @since VCafe 3.0
  171.      */
  172.  
  173.     public static void setWriter(OutputStream stream)
  174.     {
  175.         if(writer == null)
  176.         {
  177.             setWriter(new OutputStreamWriter(stream));
  178.         }
  179.     }
  180.  
  181.     /**
  182.      * @param file TODO
  183.      * @exception java.io.IOException
  184.      * @since VCafe 3.0
  185.      */
  186.  
  187.     public static void setWriter(File file)
  188.         throws IOException
  189.     {
  190.         if(writer == null)
  191.         {
  192.             setWriter(new FileWriter(file));
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * @param url TODO
  198.      * @exception java.io.IOException
  199.      * @since VCafe 3.0
  200.      */
  201.  
  202.     public static void setWriter(URL url)
  203.         throws IOException
  204.     {
  205.         if(writer == null)
  206.         {
  207.             if(url.getProtocol().equals("file"))
  208.             {
  209.                 setWriter(new FileWriter(url.getFile()));
  210.             }
  211.             else
  212.             {
  213.                 throw new IOException("URL is not a local file");
  214.             }
  215.         }
  216.     }
  217.  
  218.     /**
  219.      * @param descriptor TODO
  220.      * @since VCafe 3.0
  221.      */
  222.  
  223.     public static void setWriter(FileDescriptor descriptor)
  224.     {
  225.         if(writer == null)
  226.         {
  227.             setWriter(new FileWriter(descriptor));
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * @param f TODO
  233.      * @since VCafe 3.0
  234.      */
  235.  
  236.     public static void setInformationOff(boolean f)
  237.     {
  238.         informationOff = f;
  239.     }
  240.  
  241.     /**
  242.      * @param f TODO
  243.      * @since VCafe 3.0
  244.      */
  245.  
  246.     public static void setWarningOff(boolean f)
  247.     {
  248.         warningOff = f;
  249.     }
  250.  
  251.     /**
  252.      * @param f TODO
  253.      * @since VCafe 3.0
  254.      */
  255.  
  256.     public static void setErrorOff(boolean f)
  257.     {
  258.         errorOff = f;
  259.     }
  260.  
  261.     /**
  262.      * @param f TODO
  263.      * @since VCafe 3.0
  264.      */
  265.  
  266.     public static void setExceptionOff(boolean f)
  267.     {
  268.         exceptionOff = f;
  269.     }
  270.  
  271.     /**
  272.      * @param f TODO
  273.      * @since VCafe 3.0
  274.      */
  275.  
  276.     public static void setTraceOff(boolean f)
  277.     {
  278.         traceOff = f;
  279.     }
  280.  
  281.     /**
  282.      * @param f TODO
  283.      * @since VCafe 3.0
  284.      */
  285.  
  286.     public static void setAssertOff(boolean f)
  287.     {
  288.         assertOff = f;
  289.     }
  290.  
  291.     /**
  292.      * @since VCafe 3.0
  293.      */
  294.  
  295.     public static boolean isInformationOff()
  296.     {
  297.         return (informationOff);
  298.     }
  299.  
  300.     /**
  301.      * @since VCafe 3.0
  302.      */
  303.  
  304.     public static boolean isWarningOff()
  305.     {
  306.         return (warningOff);
  307.     }
  308.  
  309.     /**
  310.      * @since VCafe 3.0
  311.      */
  312.  
  313.     public static boolean isErrorOff()
  314.     {
  315.         return (errorOff);
  316.     }
  317.  
  318.     /**
  319.      * @since VCafe 3.0
  320.      */
  321.  
  322.     public static boolean isExceptionOff()
  323.     {
  324.         return (exceptionOff);
  325.     }
  326.  
  327.     /**
  328.      * @since VCafe 3.0
  329.      */
  330.  
  331.     public static boolean isTraceOff()
  332.     {
  333.         return (traceOff);
  334.     }
  335.  
  336.     /**
  337.      * @since VCafe 3.0
  338.      */
  339.  
  340.     public static boolean isAssertOff()
  341.     {
  342.         return (assertOff);
  343.     }
  344.  
  345.     /**
  346.      * @param clazz TODO
  347.      * @param version TODO
  348.      * @since VCafe 3.0
  349.      */
  350.  
  351.     public static void startLog(Class clazz, String version)
  352.     {
  353.         if(writer != null)
  354.         {
  355.             writer.println("Platform Information:");
  356.             writer.println("   - OS          : " + System.getProperty("os.name"));
  357.             writer.println("   - Version     : " + System.getProperty("os.version"));
  358.             writer.println("   - Platform    : " + System.getProperty("os.arch"));
  359.             writer.println("   - JDK Version : " + System.getProperty("java.version"));
  360.             writer.println("   - JDK Vendor  : " + System.getProperty("java.vendor"));
  361.             writer.println();
  362.             writer.println("Program Information:");
  363.             writer.println("   - Main Class : " + clazz.getName());
  364.             writer.println("   - Version    : " + version);
  365.             writer.println();
  366.             writer.println("Start of debugging log - " +
  367.                            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
  368.                                      .format(Calendar.getInstance().getTime()));
  369.         }
  370.     }
  371.  
  372.     /**
  373.      * @since VCafe 3.0
  374.      */
  375.  
  376.     public static void finishLog()
  377.     {
  378.         if(writer != null)
  379.         {
  380.             writer.println();
  381.             writer.println("End of debugging log - " +
  382.                            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
  383.                                      .format(Calendar.getInstance().getTime()));
  384.             writer.close();
  385.             writer = null;
  386.         }
  387.     }
  388.  
  389.     /**
  390.      * @param msg TODO
  391.      * @since VCafe 3.0
  392.      */
  393.  
  394.     public static void logInformation(String msg)
  395.     {
  396.         if(!(isInformationOff()))
  397.         {
  398.             log(INFORMATION, getExecutionContext(1), msg);
  399.         }
  400.     }
  401.  
  402.     /**
  403.      * @param msg TODO
  404.      * @since VCafe 3.0
  405.      */
  406.  
  407.     public static void logWarning(String msg)
  408.     {
  409.         if(!(isWarningOff()))
  410.         {
  411.             log(WARNING, getExecutionContext(1), msg);
  412.         }
  413.     }
  414.  
  415.     /**
  416.      * @param msg TODO
  417.      * @since VCafe 3.0
  418.      */
  419.  
  420.     public static void logError(String msg)
  421.     {
  422.         if(!(isErrorOff()))
  423.         {
  424.             log(ERROR, getExecutionContext(1), msg);
  425.         }
  426.     }
  427.  
  428.     /**
  429.      * @param ex TODO
  430.      * @since VCafe 3.0
  431.      */
  432.  
  433.     public static void logException(Throwable ex)
  434.     {
  435.         if(!(isExceptionOff()))
  436.         {
  437.             log(EXCEPTION, getExecutionContext(1), ex.getClass().getName() + " : " + ex.getMessage());
  438.  
  439.             if(writer != null)
  440.             {
  441.                 ex.printStackTrace(writer);
  442.                 writer.flush();
  443.             }
  444.         }
  445.     }
  446.  
  447.     /**
  448.      * @param action TODO
  449.      * @since VCafe 3.0
  450.      */
  451.     public static void logTrace(String action)
  452.     {
  453.         if(!(isTraceOff()))
  454.         {
  455.             ExecutionContext context;
  456.  
  457.             context = getExecutionContext(1);
  458.             log(TRACE, context, action + " " + context.getClazz() + " " + context.getMethod());
  459.         }
  460.     }
  461.  
  462.     /**
  463.      * @param type TODO
  464.      * @param context TODO
  465.      * @param msg TODO
  466.      * @since VCafe 3.0
  467.      */
  468.  
  469.     protected static void log(int type, ExecutionContext context, String msg)
  470.     {
  471.         if(writer != null)
  472.         {
  473.             writer.print(typeToString(type) + " - ");
  474.             writer.print(context + " - ");
  475.             writer.println(msg);
  476.             writer.flush();
  477.         }
  478.     }
  479.  
  480.     protected static String typeToString(int type)
  481.     {
  482.         switch(type)
  483.         {
  484.             case INFORMATION:
  485.             {
  486.                 return ("INFORMATION");
  487.             }        
  488.             case WARNING:
  489.             {
  490.                 return ("WARNING");
  491.             }        
  492.             case ERROR:
  493.             {
  494.                 return ("ERROR");
  495.             }        
  496.             case EXCEPTION:
  497.             {
  498.                 return ("EXCEPTION");
  499.             }        
  500.             case TRACE:
  501.             {
  502.                 return ("TRACE");
  503.             }
  504.         }
  505.         
  506.         return ("unknown");
  507.     }
  508.  
  509.     /**
  510.      * @since VCafe 3.0
  511.      */
  512.  
  513.     protected static String getStackTrace()
  514.     {
  515.         Throwable    stackTrace;
  516.         StringWriter writer;
  517.  
  518.         stackTrace = new Throwable().fillInStackTrace();
  519.         writer     = new StringWriter();
  520.         stackTrace.printStackTrace(new PrintWriter(writer));
  521.  
  522.         return (writer.getBuffer().toString());
  523.     }
  524.  
  525.     /**
  526.      * @param level TODO
  527.      * @since VCafe 3.0
  528.      */
  529.  
  530.     public static ExecutionContext getExecutionContext(int level)
  531.     {
  532.         return (new ExecutionContext(getStackTrace(), level));
  533.     }
  534.  
  535.     /**
  536.      * @param f TODO
  537.      * @param msg TODO
  538.      * @since VCafe 3.0
  539.      */
  540.  
  541.     public static void assert(boolean f, String msg)
  542.     {
  543.         if(!(isAssertOff()))
  544.         {
  545.             if(!f)
  546.             {
  547.                 throw new AssertionError(msg);
  548.             }
  549.         }
  550.     }
  551. }